package com.nbjtjc.util;

import com.deepoove.poi.util.TableTools;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.springframework.beans.BeanUtils;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

public class WordPoiUtil {


    public static void addTable(){
        XWPFDocument doc=new XWPFDocument();
        XWPFTable table=doc.createTable(10,13);

        XWPFTableRow row=table.getRow(0);
        List<XWPFTableCell> tableCells= row.getTableCells();
        System.out.println(tableCells.size());

        for(int i=0;i<13;i++){
            XWPFTableCell cell= row.getCell(i);
            cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
            cell.setText(i+"单元格1");
        }
        for(int i=0;i<13;i++){
            XWPFTableCell cell= row.getCell(i);
            cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
            cell.setText(i+"单元格2");
        }
        mergeCellsHorizontal(table,0,0,2);
//        TableTools.mergeCellsVertically(table,0,0,2);
//        TableTools.mergeCellsHorizonal(table,0,1,2);
//        TableTools.mergeCellsHorizonal(table,0,4,9);
//        TableTools.mergeCellsVertically(table,2,0,1);
//        TableTools.mergeCellsVertically(table,3,0,1);
        try {
//            File f=new File("/Users/guqianbin/Desktop/"+Math.random()+".docx");
            File f=new File("D:\\"+Math.random()+".docx");
            if(f.exists()==false){
                f.createNewFile();
            }
            FileOutputStream out = new FileOutputStream(f);
            doc.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        NiceXWPFDocument main=new NiceXWPFDocument(new FileInputStream("/Users/guqianbin/Desktop/fxd.docx"));
        NiceXWPFDocument sub=new NiceXWPFDocument(new FileInputStream("/Users/guqianbin/Desktop/fxd2.docx"));
        NiceXWPFDocument newDoc=main.merge(sub);
        FileOutputStream out = new FileOutputStream("/Users/guqianbin/Desktop/new_doc.docx");
        newDoc.write(out);
        newDoc.close();
        out.close();
    }
    private static void createCellsAndCopyStyles(XWPFTableRow targetRow, XWPFTableRow sourceRow) {
        targetRow.getCtRow().setTrPr(sourceRow.getCtRow().getTrPr());
        List<XWPFTableCell> tableCells = sourceRow.getTableCells();
        if (CollectionUtils.isEmpty(tableCells)) {
            return;
        }
        for (XWPFTableCell sourceCell : tableCells) {
            XWPFTableCell newCell = targetRow.addNewTableCell();
            newCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr());
            List<XWPFParagraph> sourceParagraphs = sourceCell.getParagraphs();
            if (CollectionUtils.isEmpty(sourceParagraphs)) {
                continue;
            }
            XWPFParagraph sourceParagraph = sourceParagraphs.get(0);
            List<XWPFParagraph> targetParagraphs = newCell.getParagraphs();
            if (CollectionUtils.isEmpty(targetParagraphs)) {
                XWPFParagraph p = newCell.addParagraph();
                p.getCTP().setPPr(sourceParagraph.getCTP().getPPr());
                XWPFRun run = p.getRuns().isEmpty() ? p.createRun() : p.getRuns().get(0);
                run.setFontFamily(sourceParagraph.getRuns().get(0).getFontFamily());
            } else {
                XWPFParagraph p = targetParagraphs.get(0);
                p.getCTP().setPPr(sourceParagraph.getCTP().getPPr());
                XWPFRun run = p.getRuns().isEmpty() ? p.createRun() : p.getRuns().get(0);
                run.setFontFamily(sourceParagraph.getRuns().get(0).getFontFamily());
            }
        }
    }
    /**
     * word单元格列合并
     * @param table 表格
     * @param row 合并列所在行
     * @param startCell 开始列
     * @param endCell 结束列
     * @date 2020年4月8日 下午4:43:54
     */
    public static void mergeCellsHorizontal(XWPFTable table, int row, int startCell, int endCell) {
        for (int i = startCell; i <= endCell; i++) {
            XWPFTableCell cell = table.getRow(row).getCell(i);
            if (i == startCell) {
                // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
            } else {
                // Cells which join (merge) the first one, are set with CONTINUE
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
            }
        }
    }
    public static void mergeCellsVertically(XWPFTable table, int col, int startRow, int endRow) {
        for (int i = startRow; i <= endRow; i++) {
            XWPFTableCell cell = table.getRow(i).getCell(col);
            if (i == startRow) {
                // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
            } else {
                // Cells which join (merge) the first one, are set with CONTINUE
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
            }
        }
    }

}
